package com.bjsxt.users.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.users.dao.IAAdminsDao;
import com.bjsxt.users.pojo.AAdmins;
import com.bjsxt.users.service.IUsersService;

/**
 * 用户相关的Service
 * 管理员,用户,用户扩展表
 * 
 * 一个Service持有多个DAO
 * 
 * @author wangshSxt
 * 
 * @Service("usersService")
 * <bean id="usersService" class="com.bjsxt.users.service.impl.UsersServiceImpl"/>
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 验证邮箱的唯一,服务器端验证 ,Service验证*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", admins.getEmail());
		AAdmins adminsRes = this.findOneAdminsService(condMap);
		if(adminsRes != null)
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", "邮箱已经存在,请更换邮箱");
			return resultJSON ; 
		}
		
		int res = this.adminsDao.insert(admins);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.update(admins);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AAdmins> adminsList = new ArrayList<AAdmins>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			adminsList = this.adminsDao.findCondList(condMap);
			/* adminsList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			adminsList = this.adminsDao.findCondList(condMap);
		}else
		{
			//不分页查询
			adminsList = this.adminsDao.findCondList(condMap);
		}
		return adminsList;
	}

}
